#include <func.h>
#define N 20
int add;
void *thfunc(void *p)
{
    printf("I am subthread!\n");
    int i;
    printf("subadd=%d\n",add);
    pthread_mutex_t *pmutex=(pthread_mutex_t*)p;
    for(i=0;i<N;++i)
    {
    printf("main add=%d\n",add);
        pthread_mutex_lock(pmutex);
        add++;
        pthread_mutex_unlock(pmutex);
    }
}
int main(int argc,char *argv[])
{
    int ret;
    int i;
    pthread_t pthid;
    pthread_mutex_t mutex;
    pthread_mutex_t *pmutex=(pthread_mutex_t *)&mutex;
    ret=pthread_mutex_init(pmutex,NULL);
    printf("main add=%d\n",add);
    ret=pthread_create(&pthid,NULL,thfunc,(void*)&mutex);
    THREAD_ERROR_CHECK(ret,"pthread_create");
    printf("I am main thread\n");
    sleep(1);
    for(i=0;i<N;++i)
    {
    printf("main add=%d\n",add);
        pthread_mutex_lock(pmutex);
        add++;
        pthread_mutex_unlock(pmutex);
    }
    //pthread_cancel(pthid);
    //THREAD_ERROR_CHECK(ret,"pthread_cancel");
    pthread_join(pthid,NULL);
    printf("main add=%d\n",add);
    THREAD_ERROR_CHECK(ret,"pthread_join");
    return 0;
}

